package com.frxuexi.base.mvi

import android.app.DatePickerDialog
import android.app.DatePickerDialog.OnDateSetListener
import android.app.TimePickerDialog
import android.app.TimePickerDialog.OnTimeSetListener
import android.os.Bundle
import android.widget.DatePicker
import android.widget.TimePicker
import androidx.appcompat.app.AppCompatActivity
import androidx.databinding.ViewDataBinding
import java.util.Calendar

abstract class BaseActivity<VDB : ViewDataBinding, UiIntent : IUiIntent, VM : BaseViewModel<UiIntent>> :
    AppCompatActivity() {

    lateinit var binding: VDB
    lateinit var vm: VM
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = getBind()
        setContentView(binding.root)
        vm = getVM()
        initView()
        initData()
    }

    abstract fun getVM(): VM

    abstract fun getBind(): VDB

    abstract fun initData()

    abstract fun initView()

    fun showDateDialog(call: (String) -> Unit) {
        val calendar = Calendar.getInstance()
        DatePickerDialog(
            this,
            object : OnDateSetListener {
                override fun onDateSet(view: DatePicker?, year: Int, month: Int, dayOfMonth: Int) {
                    val m = if (month + 1 < 10) "0${month + 1}" else "${month + 1}"
                    val d = if (dayOfMonth < 10) "0${dayOfMonth}" else "${dayOfMonth}"
                    call("${year}-${m}-${d}")
                }
            },
            calendar.get(Calendar.YEAR),
            calendar.get(Calendar.MONTH),
            calendar.get(Calendar.DAY_OF_MONTH)
        ).show()
    }

    fun showTimeDialog(call: (String) -> Unit) {
        TimePickerDialog(this,object :OnTimeSetListener{
            override fun onTimeSet(view: TimePicker?, hourOfDay: Int, minute: Int) {
                val h = if (hourOfDay < 10) "0${hourOfDay}" else "${hourOfDay}"
                val m = if (minute < 10) "0${minute}" else "${minute}"
                call("${h}:${m}:00")
            }
        },0,0,true).show()
    }
}